package com.ayf.payment.game.api.utils;

import java.security.*;
import java.util.HashMap;
import java.util.Map;

public class KeyUtil {

    public static String ALGORITHM = "RSA";
    public static PrivateKey priKey;
    public static PublicKey pubKey;
    public static String PUBLIC_KEY = "PublicKey";
    public static String PRIVATE_KEY = "PrivateKey";

    public static Map<String, String> genKey() {
        KeyPairGenerator kpg = null;
        try {
            kpg = KeyPairGenerator.getInstance(ALGORITHM);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
		assert kpg != null;
		kpg.initialize(2048);
        KeyPair kep = kpg.generateKeyPair();
        priKey = kep.getPrivate();
        pubKey = kep.getPublic();
        Map<String, String> param = new HashMap<String, String>(16);
        param.put(PUBLIC_KEY, new String(Base64.encode(pubKey.getEncoded())));
        param.put(PRIVATE_KEY, new String(Base64.encode(priKey.getEncoded())));
        return param;
    }
}

